package com.bdqn.controller;

import com.bdqn.pojo.Sys_user;
import com.bdqn.service.Sys_userService;
import com.bdqn.util.FileUploadUtil;
import com.bdqn.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin  //允许跨域访问
@RequestMapping("/user")
public class Sys_userController {
    @Autowired
    Sys_userService userService;
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    FileUploadUtil fileUploadUtil;
    /**
     * 登录验证，根据用户名和密码查询用户
     * 验证成功后返回加密后的字符串
     */
    @PostMapping("/checkLogin")
    public String checkLogin(@RequestBody Map<String,String> map){
        String token=null;
        //获取用户名和密码
        String username=map.get("username");
        String password=map.get("password");
        //调用service,根据用户名和密码获取用户
        Sys_user sysUser=userService.getUser(username,password);
        if (sysUser!=null){
            //登录成功，生成token字符串
            token=tokenUtil.createToken(sysUser);
        }
        System.out.println("Token是"+token);
        return token;
    }

    @GetMapping("/getUser")
    public Sys_user getUser(@RequestHeader("token") String token){
        //解析token，获取用户编号
        int uid=tokenUtil.getUid(token);
        Sys_user user=userService.getUser(uid);
        System.out.println("getuserToken"+uid);
        return user;
    }
}
